package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.klinker.android.link_builder.Link;
import com.klinker.android.link_builder.LinkBuilder;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.itemSpeakerAdapter;
import com.taptech.doufu.bean.SpeakerWallBean;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.WaitDialog;

import java.util.ArrayList;
import java.util.List;


public class SpeakerWallActivity extends DiaobaoBaseActivity implements View.OnClickListener, HttpResponseListener {


    private TextView mSpeakerWallSendOne;
    private TextView mSpeakerWallCurrentInfo;
    private ListView mListView;
    private TextView mSpeakerWallInfoTime;




    private WaitDialog dialog;
    private itemSpeakerAdapter adapter;
    private String record_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speaker_wall_layout);
        initView();
        initEvent();
        initData();

    }







    private void initView() {
        mSpeakerWallSendOne = (TextView) findViewById(R.id.speaker_wall_send_one);
        mSpeakerWallCurrentInfo = (TextView) findViewById(R.id.speaker_wall_current_info);
        mSpeakerWallCurrentInfo.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        mListView = (ListView) findViewById(R.id.speaker_wall_list);
        mSpeakerWallInfoTime = (TextView) findViewById(R.id.speaker_wall_list_info_time);

    }
    private void initEvent() {
        mSpeakerWallSendOne.setOnClickListener(this);
    }
    private void initData() {
        dialog = new WaitDialog(this, R.style.updateDialog);
        dialog.show();

        Intent intent = getIntent();
        record_id = intent.getStringExtra(Constant.RECORDID);

        PersonalInfoService.getInstance().getSpeakerWallInfo(this, record_id);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.speaker_wall_send_one:
                if (AccountService.getInstance().isLogin()) {
                    Intent intent =  new Intent(this,MySpeakerActivity.class);
                    startActivity(intent);
                }else {
                    AccountService.getInstance().jumpToLogin();
                }
                break;

            default:
                break;
        }
    }



    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {


        try {
            if (response == null){
                UIUtil.toastMessage(this, Constant.loadingFail);
                return;
            }

            switch (handleType){
                case PersonalInfoService.HANDLE_TYPE_GET_SPEAKER_WALL_INFO:
                    if (response.getStatus() == 0){
                        SpeakerWallBean speakerWallBean = SpeakerWallBean.getSpeakerWallBean(response.getData().toString());
                        mSpeakerWallInfoTime.setText(speakerWallBean.getDesc());
                        adapter = new itemSpeakerAdapter(this,R.layout.activity_speaker_wall_item, speakerWallBean.getList());
                        mListView.setAdapter(adapter);

                        int splitSize = speakerWallBean.getCurrent_info().getMsg().size();
                        List<SpeakerWallBean.CurrentInfoBean.MsgBean> msgBean= speakerWallBean.getCurrent_info().getMsg();

                        List<Link> links = new ArrayList<>();
                        for (int i = 0; i < splitSize; i++) {
                            mSpeakerWallCurrentInfo.append(msgBean.get(i).getSplit_text());
                            if (msgBean.get(i).getUid() != null) {
                                links.add(setLink(msgBean.get(i).getUid(),msgBean.get(i).getSplit_text(),msgBean.get(i).getDay_color()));
                            }
                        }

                        if (links.size() > 0) {
                            LinkBuilder.on(mSpeakerWallCurrentInfo).addLinks(links).build();
                        }

                    }else {
                        UIUtil.toastMessage(this, "网络异常，请检查网络后再试");
                    }
                    break;

            }
        } catch (Exception e) {
            UIUtil.toastMessage(this, "网络异常，请检查网络后再试");
            e.printStackTrace();
        }
        dialog.cancel();
    }

    private Link setLink(final String uuid, final String userName,String color) {
        Link link = new Link(userName);
        link.setUnderlined(false);
        link.setTextColor(Color.parseColor("#" + color));
        link.setOnClickListener(new Link.OnClickListener() {
            @Override
            public void onClick(String clickedText) {
                startLink(uuid);
            }
        });
        return link;
    }


    private void startLink(String uuid) {
        UserDetailActivity.Companion.startActivity(this,uuid);
    }


}
